﻿using HuaWei;
using HuaWei.BusinessRule;
using HuaWei.CommClass;
using HuaWei.Common;
using HuaWei.DAI;
using HuaWei.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;

internal static class Class38
{
    public static IDataAccess smethod()
    {
        string str = IniFile.ReadIniFile(GParams.SysFilePath, "SYS", "Server", false);
        if (str.Equals(""))
        {
            str = "127.0.0.1:8066";
        }
        string wcfAddr = string.Format("http://{0}/WCFService", str);
        CBaseRule rule = new CBaseRule();
        return rule.GetWCF(wcfAddr);
    }

    public static bool smethod1(string _str, DataSet dataSet, string _str1, string _str2, string _str3, [Optional, DefaultParameterValue("")] string _str4)
    {
        try
        {
            IDataAccess access = smethod();
            if (access != null)
            {
                List<string> list = new List<string>();
                list.Add(_str1);
                list.Add(_str2);
                list.Add(_str3);
                string str = access.Invoke(dataSet, _str, CHelp.ListToParam(list));
                if (!str.Contains("ERROR: "))
                {
                    if (_str4.Equals(""))
                    {
                        MsgBox.ShowMsg("操作已完成！");
                    }
                    else if (!_str4.Equals("Null"))
                    {
                        MsgBox.ShowMsg(_str4);
                    }
                    return true;
                }
                MsgBox.ShowMsg(str.Substring("ERROR: ".Length));
                return false;
            }
        }
        catch (Exception exception)
        {
            Log.Write(exception.Message);
            if (!_str4.Equals("Null"))
            {
                MsgBox.ShowError("请求服务器时出错，请查检网络是否连通！", exception);
                return false;
            }
        }
        return true;
    }

    public static bool RobackDataValidate()
    {
        try
        {
            IDataAccess access = smethod();
            if (access != null)
            {
                List<string> list = new List<string>();
                list.Add(GParams.LoginId);
                list.Add(GParams._BDID);
                list.Add(GParams.QCInfo.QCID);
                string str = access.Invoke(null, "RobackDataValidate", CHelp.ListToParam(list));
                if (string.IsNullOrEmpty(str))
                {
                    return true;
                }
                else
                {
                    MsgBox.ShowMsg(str);
                    return false;
                }
            }
            else
            {
                MsgBox.ShowMsg("实例化数据访问出错！");
                return false; 
            }
        }
        catch (Exception exception)
        {
            Log.Write(exception.Message);
            MsgBox.ShowError("请求服务器时出错，请查检网络是否连通！", exception);
            return false;
        }
    }

    public static bool smethod2(string _str)
    {
        return smethod1("ModifyUpdateStatus", null, _str, "", "", "Null");
    }

    public static int smethod3(string _str, string _str1)
    {
        IDataAccess access;
        string computerSerialNumber = SysUtils.GetComputerSerialNumber();
        DataSet set = null;
        try
        {
            access = smethod();
            List<string> list = new List<string>();
            list.Add(_str);
            list.Add(_str1);
            list.Add(computerSerialNumber);
            set = access.GetDs(null, "CheckUser", CHelp.ListToParam(list));
            if (set == null)
            {
                MsgBox.ShowMsg("当前用户已在别的电脑注册，请与管理员联系!");
                return -1;
            }
        }
        catch (Exception exception)
        {
            Log.Write(exception.Message);
            return 0;
        }
        DataTable table = set.Tables["YHXX"];
        if (table.Rows.Count > 0)
        {
            int length;
            DataRow row = table.Rows[0];
            if (!row["YHMC"].Equals(_str) || !row["YHMM"].Equals(_str1))
            {
                return 0;
            }
            GParams.LoginId = row["bdid"].ToString();
            string o = "";
            if (_str.Equals("admin"))
            {
                length = 3;
                o = length.ToString();
            }
            else
            {
                o = row["jglx"].ToString();
            }
            GParams._JGLX = (JGLX)CHelp.ConvertInt(o, 0);
            string filename = GParams.APP_PATH + "system.ini";
            string keyValue = EncryptDecrypt.smethod1(GParams.LoginId, "DdTtMmJl");
            string str5 = EncryptDecrypt.smethod1(o, "DdTtMmJl");
            IniFile.WriteIniFile(filename, "SYS", "MID", keyValue, false);
            IniFile.WriteIniFile(filename, "SYS", "MLX", str5, false);
            access = DBHelp.IniIDA();
            DataSet ds = new DataSet();
            string sql = string.Format("select * from YHXX where bdid='{0}'", GParams.LoginId);
            DBHelp.SetDs(ds, sql, "YHXX", access);
            length = ds.Tables["YHXX"].Select(string.Format("YHMC='{0}'", _str)).Length;
            if (length.Equals(0))
            {
                DBHelp.ExeSql(string.Format("insert into yhxx (id,bdid,yhmc,yhmm)  values('{0}','{1}','{2}','{3}')", new object[] { Guid.NewGuid(), GParams.LoginId, _str, _str1 }));
            }
            else
            {
                DBHelp.ExeSql(string.Format("update yhxx set yhmm='{0}' where bdid='{1}' and yhmc='{2}'", _str1, GParams.LoginId, _str));
            }
            return 1;
        }
        MsgBox.ShowMsg("用户名或密码不正确，请重新输入！");
        return -2;
    }

    public static bool YfQclfhSo(string _str, string _str1, string _str2)
    {
        return smethod1("CancelUpdate", null, _str, _str1, _str2, "");
    }
}

